/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xos_net.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../helper_objs/head.h"
#include "../data_struct/head.h"
#include "../config/head.h"
#include "../macro/head.h"
#include "../impl/head.h"
#include "../impl/head.h"
#include "../msg/head.h"
#include "msg_main.h"

namespace xos_net
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    msg_main::msg_main()
    {
    }

    msg_main::~msg_main()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // 
    // proc
    // 
    int msg_main::proc( xos_box::i_msg*& pMsg )
    {
        int ret = 1;

        int nMsg = pMsg->get_msg();

        switch( nMsg )
        {
            // 
            // release connect
            // 
            case MAIN_RELEASE_CONNECTION:
            {
                release_connect( pMsg );
            }
            break;

            // 
            // connect stopped
            // 
            case MAIN_ALL_CONNECT_STOPPED:
            {
                all_connect_stopped();
            }
            break;
            case MAIN_ALL_SERVER_STOPPED:
            {
                all_server_stopped();
            }
            break;

            // 
            // startting
            // 
            case MAIN_STARTING:
            {
                startting();
            }
            break;
            case MAIN_START_FAILED:
            {
                start_failed();
            }
            break;
            case MAIN_STARTED:
            {
                startted();
            }
            break;

            // 
            // running
            // 
            case MAIN_HEART:
            {
                heart();
            }
            break;

            // 
            // stopping
            // 
            case MAIN_STOPPING:
            {
                stopping();
            }
            break;
            case MAIN_STOPPED:
            {
                stopped();
            }
            break;

            // 
            // other
            // 
            default:
            {
                ret = 0;
            }
            break;
        }

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int msg_main::release_connect( xos_box::i_msg *& pMsg )
    {
        int ret = 0;
        connection * pConnect = (connection *)pMsg->get_void( 0, 0 );
        bool bTest = pConnect->tcp_listen() == connection::done_test_server();
        xos_stl::release_interface( pConnect );
        if( bTest )
        {
            connection::done_test()->test( 1 );
        }
        return ret;
    }

    int msg_main::all_server_stopped()
    {
        int ret = 0;
        connection::stop_all_connect();
        return ret;
    }

    int msg_main::all_connect_stopped()
    {
        int ret = 0;
        helper_main::get()->notify( MAIN_STOPPED );
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int msg_main::startting()
    {
        int ret = 0;
        return ret;
    }

    int msg_main::start_failed()
    {
        int ret = 0;
        return ret;
    }

    int msg_main::startted()
    {
        int ret = 0;
        return ret;
    }

    int msg_main::heart()
    {
        int ret = 0;
        connection::heart();
        return ret;
    }

    int msg_main::stopping()
    {
        int ret = 0;
        helper_callback::get()->notify( net::NET_STOPPING );
        connection::stop_all_server();
        return ret;
    }

    int msg_main::stopped()
    {
        int ret = 0;
        helper_callback::get()->notify( net::NET_STOPPED );
        return ret;
    }

} // xos_net
